package com.jzy.stock.kufangkuqu.service;

import com.jzy.stock.kufangkuqu.mapper.KuFangKuQuMapper;
import com.jzy.stock.kufangkuqu.model.*;
import com.jzy.stock.util.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by JZZK on 2022/7/11.
 */
@Service
public class KuFangKuQuServiceImpl implements KuFangKuQuService {

    @Autowired
    private KuFangKuQuMapper kuFangKuQuMapper;

    //分页
    @Override
    public ResultVo queryPage(SelectVo selectVo) {

        // 查询数据
        List<ListVo> listVos = kuFangKuQuMapper.queryPage(selectVo);
        // 给前端小类状态名称赋值
        for (ListVo listVo : listVos) {
            if (listVo.getStatus() != null) {
                Integer status = listVo.getStatus() ? 1:0;
                listVo.setStatusName(StatusEnum.queryById(status));
            }
        }
        // 查询数量
        int count = kuFangKuQuMapper.queryCount(selectVo);
        // 把数据返回前端
        AllRecordsVo allRecordsVo = new AllRecordsVo();
        // 1.返回所有小类信息
        allRecordsVo.setDataList(listVos);
        // 2.返回小类总数量  计算分页
        allRecordsVo.setTotalNumber(count);
        // 3.当前页赋值
        allRecordsVo.setPageIndex(selectVo.getPageIndex());
        // 4.给每页大小赋值
        allRecordsVo.setPageSize(selectVo.getPageSize());
        // 5.给总页数赋值
        allRecordsVo.countPage(count);
        return ResultBuildVo.success(allRecordsVo);
    }

    //修改状态
    @Override
    public ResultVo updateStatus(@Valid UpdateStatusVo updateStatusVo) {
        ListVo listVo = kuFangKuQuMapper.queryById(updateStatusVo.getId());
        if (listVo == null) {
            return ResultBuildVo.error("修改状态数据不存在");
        }
        String name = "张三";
        Date nowDate = new Date();
        ListVo listVo1 = new ListVo();
        listVo1.setUpdateName(name);
        listVo1.setUpdateTime(nowDate);
        // BeanUtils.copyProperties 把左边对象的参数值 赋值给右边对象的参数值，注意：参数名称必须一致，参数类型必须一致。
        BeanUtils.copyProperties(updateStatusVo, listVo1);
        kuFangKuQuMapper.updateStatus(listVo1);
        return ResultBuildVo.success();

    }

    //新增
    @Override
    @Transactional
    public ResultVo add(@Valid AddVo addVo) {
        // 1.验证一个库房下库区编码是否重复
        ListVo listVo = kuFangKuQuMapper.queryByCode(addVo.getCode(),addVo.getWareHouseCode());
        if (listVo != null) {
            return ResultBuildVo.error("一个库房下库区编码重复");
        }
        // 2.验证一个库房下库区名称是否重复
        ListVo listVo1 = kuFangKuQuMapper.queryByName(addVo.getName(),addVo.getWareHouseCode());
        if (listVo1 != null) {
            return ResultBuildVo.error("一个库房下库区名称重复");
        }
        ListVo listVo2 = new ListVo();
        Date nowDate = new Date();
        // 3.给新增 修改时间赋值
        listVo2.setCreateTime(nowDate);
        listVo2.setUpdateTime(nowDate);
        // 4.给新增 修改人赋值  一般是用session里面的名称赋值，这里没有直接写死
        listVo2.setCreateName("session里面的名称");
        listVo2.setUpdateName("session里面的名称");
        //BeanUtils.copyProperties
        //把左边对象的参数值 赋值给右边对象的参数值，注意：参数名称必须一致，参数类型必须一致。
        BeanUtils.copyProperties(addVo, listVo2);
        //新增库区表
        kuFangKuQuMapper.addVo(listVo2);
        //新增库区属性中间表
        kuFangKuQuMapper.addVo2(listVo2);
        return ResultBuildVo.success();

    }

    //修改
    @Override
    @Transactional
    public ResultVo update(@Valid UpdateVo updateVo) {
        //验证修改库区数据是否存在
        ListVo listVo = kuFangKuQuMapper.queryByCode(updateVo.getCode(), updateVo.getWareHouseCode());
        if (listVo == null){
            return ResultBuildVo.error("修改库区数据不存在");
        }
        //验证同一库房下名称是否重复
        ListVo listVo1 = kuFangKuQuMapper.queryByName(updateVo.getName(),updateVo.getWareHouseCode());
        if (listVo1 != null){
            return ResultBuildVo.error("名称已存在！");
        }
        //必须是禁用状态才能修改
        if (listVo.getStatus()){
            return ResultBuildVo.error("必须是禁用状态才能修改");
        }
        //给修改时间 修改人赋值
        Date nowDate = new Date();
        ListVo listVo2 = new ListVo();
        listVo2.setUpdateName("国");
        listVo2.setUpdateTime(nowDate);
        BeanUtils.copyProperties(updateVo, listVo2);
        //修改库区
        kuFangKuQuMapper.update(listVo2);
        //修改业务属性   先删除再批量新增进去
        kuFangKuQuMapper.delete(listVo2.getCode());
        kuFangKuQuMapper.addVo2(listVo2);
        return ResultBuildVo.success();
    }

    //新增库房下拉框
    @Override
    public ResultVo queryWareHouse() {
        List<WareHouseVo> wareHouseVoList = kuFangKuQuMapper.queryWareHouse();
        return ResultBuildVo.success(wareHouseVoList);
    }

    //新增库区业务属性下拉框
    @Override
    public ResultVo queryBusinessAttribute() {
        List<BusinessAttributeVo> businessAttributeVoList = kuFangKuQuMapper.queryBusinessAttribute();
        return ResultBuildVo.success(businessAttributeVoList);
    }

    //查看
    @Override
    public ResultVo queryStorageSectionById(Integer id) {
        ListVo listVos = kuFangKuQuMapper.queryStorageSectionById(id);
        List<BusinessAttributeVo> businessAttribute = kuFangKuQuMapper.queryByCode1(listVos.getCode());
        listVos.setBusinessAttribute(businessAttribute);
        return ResultBuildVo.success(listVos);
    }
}
